#include <stdio.h>

int swap(int* x, int* y);
int quickSort(int arr[], int head, int tail);

int main(int argc, const char args[])
{
	int arr[] = {4, 65, 8, 2, 13, 3, 3, 6};
	int len = sizeof(arr)/sizeof(int);
	int i;

	printf("\n==========arr[]=========\n");
	for (i = 0; i < len; ++i)
	{
		printf("%d ", arr[i]);
	}

	quickSort(arr, 0, len - 1);

	printf("\n\n==========quickSort=========\n");
	for (i = 0; i < len; ++i)
	{
		printf("%d ", arr[i]);
	}	
	return 0;
}

int swap(int* x, int* y)
{
	int temp = *x;
	*x = *y;
	*y = temp;

	return 0;
}

int quickSort(int arr[], int head, int tail)
{
	int i = head;
	int j = tail;
	int key = arr[head];

	while (i < j)
	{
		while (arr[j] >= key && i < j)
			--j;
		while (arr[i] <= key && i < j)
			++i;
		if (i < j)
			swap(&arr[i], &arr[j]);
	}
	swap(&arr[head], &arr[j]);

	if (i-1 > head)
		quickSort(arr, head, i-1);

	if (i+1 < tail)
		quickSort(arr, i+1, tail);

	return 0;
}